/*
 * @Descripttion: 
 * @version: 
 * @Author: TT
 * @Date: 2023-10-28 15:23:23
 * @LastEditors: TT-hzy
 * @LastEditTime: 2024-08-11 13:28:23
 */

import 'package:hzy_project_template/config/config_index.dart';
import 'package:hzy_project_template/controllers/user_c.dart';

import 'package:hzy_project_template/utils/utils_index.dart';

import 'tu_chong_http_config.dart';
import 'tuchong_http_transformer.dart';

class TuChongApiUtils {
  /// 获取图虫数据
  static loadtuchongData({
    int page = 1,
    int? lastPostId,
    bool showload = false,
  }) async {
    Map<String, dynamic> data = {};
    if (lastPostId != null) {
      data = {
        "post_id": lastPostId,
        "page": page,
        "type": "loadmore",
      };
    }
    return await request(
      path: TuChongHttpConfig.listUrl,
      data: data,
      httpTransformer: TuchongHttpTransformer(),
      isShowLoad: showload,
      isShowError: false,
      method: Method.get,
    );
  }

  /// 添加收藏
  static Future<HzyNormalResponse> loadAddCollect({
    dynamic data,
  }) async {
    List<Map<dynamic, dynamic>>? list = getCollectData();
    if (list != null) {
      list.removeWhere((element) => element["post_id"] == data["post_id"]);
      list.insert(0, data);
    } else {
      list = [data];
    }
    var yorn =
        await SharedPreferencesTool.putObjectList(getCollectLocalStr(), list);
    late HzyNormalResponse response;
    if (yorn == true) {
      response = HzyNormalResponse.success(
        netData: list,
        reqMsg: LaunchIdConfig.collect.tr + LaunchIdConfig.success.tr,
      );
    } else {
      response = HzyNormalResponse.fail(
        data: data,
        errorMsg: LaunchIdConfig.collect.tr + LaunchIdConfig.fail.tr,
      );
    }
    return response;
  }

  /// 获取收藏列表
  static Future<HzyNormalResponse> loadCollectListData() async {
    return HzyNormalResponse.success(
      netData: getCollectData(),
      reqMsg: "",
    );
  }

  /// 删除收藏
  static Future<HzyNormalResponse> loadDelectCollect({
    required int postId,
  }) async {
    List<Map<dynamic, dynamic>>? list = getCollectData();
    list?.removeWhere((element) => element["post_id"] == postId);
    var yorn = await SharedPreferencesTool.putObjectList(
        getCollectLocalStr(), list ?? []);
    late HzyNormalResponse response;
    if (yorn == true) {
      response = HzyNormalResponse.success(
        netData: list,
        reqMsg: LaunchIdConfig.canner.tr + LaunchIdConfig.success.tr,
      );
    } else {
      response = HzyNormalResponse.fail(
        data: list,
        errorMsg: LaunchIdConfig.canner.tr + LaunchIdConfig.fail.tr,
      );
    }
    return response;
  }

  /// 获取本地收藏数据
  static List<Map<dynamic, dynamic>>? getCollectData() {
    String collectLocalStr = getCollectLocalStr();
    List<Map<dynamic, dynamic>>? list = SharedPreferencesTool.getObjectList(
      collectLocalStr,
    );
    return list;
  }

  /// 收藏数据本地标志位
  static getCollectLocalStr() {
    return "${UserC.current.user?.account ?? '火之夜'}Collect";
  }
}
